<?php

namespace log_center\middleware;

use log_center\Log;
use think\facade\Request;

class LogMidWare
{
    public function handle($request, \Closure $next)
    {
        $response = $next($request);

        $component = strtolower(Request::module());
        $controller = strtolower(Request::controller());
        $action = strtolower(Request::action());

        $route = '/' . $controller . '/' . $action;
        $params = getEncryptData();

        $userInfo = getUserInfoByToken(Request::header('token'));
        $params['user_id'] = $userInfo[ 'user_id' ] ?? 0;

        try {
            Log::init($component)->route($route)->write($params);
        } catch (\Exception $e) {
        } catch (\Throwable $t) {
        }

        return $response;
    }
}